package com.ldy.transmit.advice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ldy.transmit.common.response.Response;
import com.ldy.transmit.common.response.ResponseHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.UnknownHttpStatusCodeException;

@RestControllerAdvice(basePackages= {"com.ldy.transmit"})
public class LdyExceptionHandler {
	
	private Logger LOGGER = LoggerFactory.getLogger(LdyExceptionHandler.class);
	
	@ExceptionHandler(HttpClientErrorException.class)
	public Response handleClientException(HttpServletRequest request, HttpServletResponse response, HttpClientErrorException exception) {
		LOGGER.error("got an http client error exception -----> ", exception);
		return ResponseHelper.buildFail(exception.getLocalizedMessage());
	}
	
	@ExceptionHandler(HttpServerErrorException.class)
	public Response handleServerException(HttpServletRequest request, HttpServletResponse response, HttpServerErrorException exception) {
		LOGGER.error("got an http server error exception -----> ", exception);
		return ResponseHelper.buildFail(exception.getLocalizedMessage());
	}
	@ExceptionHandler(UnknownHttpStatusCodeException.class)
	public Response handleUnknownHttpStatusCodeException(HttpServletRequest request, HttpServletResponse response, UnknownHttpStatusCodeException exception) {
		LOGGER.error("unknown http status code exception -----> ", exception);
		return ResponseHelper.buildFail(exception.getLocalizedMessage());
	}
}
